<template>
  <div class="container">
    <div class="mui-card"  v-for="(item,i) in goodsList" :key="item.id">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <div class="switch">
            <mt-switch v-model="$store.getters.getGoodsSelected[item.id]" @change="selectedChanged(item.id,$store.getters.getGoodsSelected[item.id])"></mt-switch>
          </div>
          <div class="img">
            <img
              :src="item.thumb_path"
              alt
            >
          </div>
          <div class="info">
            <h1>{{item.title}}</h1>
            <p class="info_contnet">
              <span class="price">￥{{item.sell_price}}</span>
              <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
            </p>
            <p class="del">
              <span @click="del(item.id,i)">删除</span>
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
            <!-- 购物车结算页面 -->
            <div class="left">
                <p>总计（不含运费）</p>
                <p class="amount">已勾选商品
                    <span class="count">{{$store.getters.getGoodsCountAndAmount.count}}</span>
                    件，总价：     
                </p>
                <p><span class="money">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span></p>
            </div>
            <div class="right">
                <mt-button type="danger" size="normal">去结算</mt-button>
            </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import shopcar_numbox from "../subcomponents/shopcar_numbox.vue";
export default {
  data() {
    return {
        goodsList:[]
    };
  },
  created(){
      this.getShopCarList();
  },
  methods: {
      getShopCarList(){
        //   1.获取到store中所有的商品id，然后拼接出一个 用逗号分割的字符串
        var idarry=[];
        this.$store.state.car.forEach(item=>idarry.push(item.id));
        if(idarry.length<=0){
            // 如果购物车中没有商品，则直接返回，不需要再请求数据接口，否则会报错
            return;
        }
        this.$http.get("api/goods/getshopcarlist/"+idarry.join(',')).then(result=>{
            if(result.body.status===0){
                this.goodsList=result.body.message;
            }
        })
      },
      del(id,i){
          console.log(i)
          this.goodsList.splice(i,1)
          this.$store.commit('remove',id)
          
         
      },
      selectedChanged(id,val){
        // 将开关的状态同步到store中去
        this.$store.commit('updateGoodsSelected',{id,selected:val})
      }
  },
  components: {
    numbox: shopcar_numbox
  },
};
</script>

<style lang="scss" scoped>
.container {
  background-color: #eee;
  overflow: hidden;
  .mui-card-content-inner {
    display: flex;
    align-items: center;
    .switch {
      height: 30px;
    }
    .img {
      height: 86px;
      width: 70px;
      margin-left: 2px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .info {
      margin-left: 20px;
      display:flex;
      flex-direction: column;
      justify-content: space-between;
      h1 {
        font-size: 13px;
      }
      .info_contnet {
        margin-top: 15px;
        display: flex;
        justify-content: space-between;
        .price {
          color: red;
          font-size: 14px;
        }
      }
      .del {
       
        span {
          color: blue;
          font-size: 13px;
        }
      }
    }
  }
  .left{
      p{
          font-size:15px;
           .money{
              color:red;
              font-size:20px;
          }
      }
      .amount{
          font-size:15px;
          .count{
              color:red;
              font-size:20px;
          }      
      }
  }
  .right{
      margin-left: 30px;
      align-items: center;
  }
}
</style>
